package com.tianqu.tms.www.statistics.service;

import java.math.BigDecimal;
import java.util.Calendar;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsSellerRegisterDayCountDao;
import com.tianqu.tms.www.statistics.dao.TmsSellerRegisterMonthCountDao;
import com.tianqu.tms.www.statistics.dto.BusinessTransactionDto;
import com.tianqu.tms.www.statistics.entity.TmsSellerRegisterDayCountDto;
import com.tianqu.tms.www.statistics.entity.TmsSellerRegisterMonthCountDto;


public class BusinessTransactionService {

	/**
	 * 实时统计
	 * @return
	 * @throws TmsDaoException
	 */
	public BusinessTransactionDto getBusinessTransactionCount()throws TmsDaoException{
	
		TmsSellerRegisterDayCountDao daoDay = new TmsSellerRegisterDayCountDao();
		TmsSellerRegisterDayCountDto dtoDay1 = daoDay.getTmsSellerRegisterDayCount();
		TmsSellerRegisterDayCountDto dtoDay2 = daoDay.getInStorehouseCount();
		TmsSellerRegisterMonthCountDto dtoMonth = daoDay.getMonthSales();
		BusinessTransactionDto dtoBt = new BusinessTransactionDto();
			
		java.text.DecimalFormat myformat=new java.text.DecimalFormat("0.0");
		// 商户总数
		dtoBt.setNumMerchants(Integer.valueOf(dtoDay1.getCnt()));
			
		// 在库车辆
		dtoBt.setInStorehouse(dtoDay2.getOnlineNum());
			
		// 车位使用率
		dtoBt.setUtilizationSpace(myformat.format(dtoDay2.getRatio())+"%");
			
		// 今日销售量
		dtoBt.setTodaySales(dtoDay2.getOfftakeNum());
			
		// 今日收车量  
		dtoBt.setTodayBuying(dtoDay2.getBuyNum());

		// 本月销售量
		dtoBt.setMonthSales(dtoMonth.getOfftakeNum());
		
		// 本月买入量
		dtoBt.setMonthBuying(dtoMonth.getBuyNum());
		
		return dtoBt;
	}
	
	/**
	 * 当月月销售量（量）同比、环比/月收车量（万元）同比、环比
	 * @return
	 * @throws TmsDaoException
	 */
	public BusinessTransactionDto getVolumeAndRatio() throws TmsDaoException{
	
		TmsSellerRegisterMonthCountDao dao = new TmsSellerRegisterMonthCountDao();
		TmsSellerRegisterDayCountDao dao2 = new TmsSellerRegisterDayCountDao();
		BusinessTransactionDto dtoBt = new BusinessTransactionDto();
		TmsSellerRegisterMonthCountDto dtoMonth1 = dao2.getMonthSales();
		BusinessTransactionDto dtoMonth2 = dao.getLastYeasMonthSales();
		BusinessTransactionDto dtoMonth3 = dao2.getMonthLastMonthSales();
		
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH )+1;
		int day =cal.get(Calendar.DAY_OF_MONTH);
		
		// 上一年月份总天数
		int yesteryearMonthDays = DateUtil.getDaysByYearMonth(year-1, month);
		
		// 上一年度同期累计卖出量 = （上一年度当前月份销售总量/月份总天数）*本月到系统当前时间的天数；
		BigDecimal offtakeNumLastYeas = dtoMonth2.getOfftakeNumLastYeas().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays),4,BigDecimal.ROUND_HALF_DOWN);
		BigDecimal buyNumLastYeas = dtoMonth2.getBuyNumLastYeas().multiply(new BigDecimal(day)).divide(new BigDecimal(yesteryearMonthDays),4,BigDecimal.ROUND_HALF_DOWN);
		
		// 当月新增同比，当月累计到系统当前日期与上一年度同期相比，即（本月累计交易过户数量 - 上一年度同期累计交易过户数量）/上一年度同期累计交易过户数量*100%，保留一位小数，四舍五入； 
		BigDecimal salesVolume = BigDecimal.ZERO;
		BigDecimal buyingVolume  = BigDecimal.ZERO;;
		
		if(BigDecimal.ZERO.compareTo(offtakeNumLastYeas) != 0) {
			
			salesVolume = BigDecimal.valueOf(dtoMonth1.getOfftakeNum()).subtract(offtakeNumLastYeas).divide(offtakeNumLastYeas,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}
		
		if(BigDecimal.ZERO.compareTo(buyNumLastYeas) != 0) {
			
			buyingVolume = BigDecimal.valueOf(dtoMonth1.getBuyNum()).subtract(buyNumLastYeas).divide(buyNumLastYeas,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));		
		}
		
		// 当月新增库存数量同比是否是增长
		if(BigDecimal.ZERO.compareTo(salesVolume) <= 0) {
			
			dtoBt.setSalesVolumeFlg(true);
		}else {
			salesVolume = new BigDecimal(-1).multiply(salesVolume);
			dtoBt.setSalesVolumeFlg(false);
		}
		
		// 当月新增库存总值同比是否是增长
		if(BigDecimal.ZERO.compareTo(buyingVolume) <= 0) {
			
			dtoBt.setBuyingVolumeFlg(true);
		}else {
			buyingVolume = new BigDecimal(-1).multiply(buyingVolume);
			dtoBt.setBuyingVolumeFlg(false);
		}
		java.text.DecimalFormat myformat=new java.text.DecimalFormat("0.0");
		
		dtoBt.setSalesVolume(myformat.format(salesVolume)+"%");
		dtoBt.setBuyingVolume(myformat.format(buyingVolume)+"%");
		
		// 当月新增环比，当月累计到系统当前日期与上一月份同期相比
					
		BigDecimal offtakeNumLastMonth = dtoMonth3.getOfftakeNumLastMonth();
		BigDecimal buyNumLastMonth = dtoMonth3.getBuyNumLastMonth();
					
	    // 即（本月累计交易过户数量 - 上一月份同期累计交易过户数量）/上一月份同期累计交易过户数量*100%，保留一位小数，四舍五入
		BigDecimal SalesVolumeRatio = BigDecimal.ZERO;
		BigDecimal BuyingVolumeRatio = BigDecimal.ZERO;
		
		 
		if(BigDecimal.ZERO.compareTo(offtakeNumLastMonth) != 0) {
	
			// 上一月份同期累计交易过户数量不为0的情况,上一月份同期累计交易过户数量 = 0,默认同比为0.0%
			SalesVolumeRatio = BigDecimal.valueOf(dtoMonth1.getOfftakeNum()).subtract(offtakeNumLastMonth).divide(offtakeNumLastMonth,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}

		if(BigDecimal.ZERO.compareTo(buyNumLastMonth) != 0) {
			
			BuyingVolumeRatio =  BigDecimal.valueOf(dtoMonth1.getBuyNum()).subtract(buyNumLastMonth).divide(buyNumLastMonth,4,BigDecimal.ROUND_HALF_DOWN).multiply(new BigDecimal(100));
		}
		
		// 当月新增库存数量环比是否是增长
		if(BigDecimal.ZERO.compareTo(SalesVolumeRatio) <= 0) {
			
			dtoBt.setSalesVolumeRatioFlg(true);
		}else {
			SalesVolumeRatio = new BigDecimal(-1).multiply(SalesVolumeRatio);
			dtoBt.setSalesVolumeRatioFlg(false);
		}
		
		// 当月新增库存总值环比是否是增长
		if(BigDecimal.ZERO.compareTo(BuyingVolumeRatio) <= 0) {
			
			dtoBt.setBuyingVolumeRatioFlg(true);
		}else {
			BuyingVolumeRatio = new BigDecimal(-1).multiply(BuyingVolumeRatio);
			dtoBt.setBuyingVolumeRatioFlg(false);
		}
		dtoBt.setSalesVolumeRatio(myformat.format(SalesVolumeRatio)+"%");
		dtoBt.setBuyingVolumeRatio(myformat.format(BuyingVolumeRatio)+"%");
		
		return dtoBt;
	}
	
}
